package handlers

import (
	"encoding/json"
	"film-hub/cache"
	"film-hub/models"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	img := c.PostForm("img")

	content := c.PostForm("content")
	data := models.Videos{

		Title:   title,
		Img:     img,
		Content: content,
	}
	redis_key := "Add_" + title
	changeData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if changeData == "" {
		models.DB.Create(&data)

		jsonData, _ := json.Marshal(data)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(changeData), &data)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}

func List(c *gin.Context) {
	var arr []models.Videos
	redis_key := "list_data_"
	changeData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if changeData == "" {
		models.DB.Find(&arr)
		jsondata, _ := json.Marshal(arr)
		cache.Rdb.Set(cache.Ctx, redis_key, jsondata, time.Hour*2)
	} else {
		json.Unmarshal([]byte(changeData), &arr)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"list": arr,
	})
}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var arr models.Videos

	redis_key := "Detail_list" + id
	changeData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if changeData == "" {
		models.DB.Where("id=?", id).First(&arr)
		jsonData, _ := json.Marshal(arr)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(changeData), &arr)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "详情成功",
		"list": arr,
	})
}
func GetList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "3"))
	offset := (page - 1) * pageSize
	var arr []models.Videos

	redis_key := "getList_"
	changeData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if changeData == "" {
		models.DB.Offset(offset).Limit(pageSize).Find(&arr)
		jsonData, _ := json.Marshal(arr)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*1)
	} else {
		json.Unmarshal([]byte(changeData), &arr)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查看列表ok",
		"list": arr,
	})
}

func Register(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户名不能为空",
		})
		return
	}
	var arr models.Users
	err := models.DB.Where("username=?", username).First(&arr).Error
	if err == nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "不能重复注册",
		})
		return
	}
	data := models.Users{

		Username: username,
		Password: password,
	}

	rides_key := "reg_ter" + username
	changData, _ := cache.Rdb.Get(cache.Ctx, rides_key).Result()
	if changData == "" {
		err = models.DB.Create(&data).Error
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, rides_key, jsonData, time.Hour*1)
	} else {
		json.Unmarshal([]byte(changData), &data)
	}
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "注册失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "注册成功",
	})
}
func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户名或密码不能为空",
		})
		return
	}
	redis_key := "login_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "密码错误大于三次,禁止登录",
		})
		return
	}
	var arr models.Users
	err := models.DB.Where("username=?", username).First(&arr).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}
	if password != arr.Password {
		cache.Rdb.Incr(cache.Ctx, redis_key)

		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 400,
				"msg":  "密码错误3次,请两小时后再尝试",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "密码错误",
		})
		return

	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
	})

}
